package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.exception.CustomException;
import com.springboot_gradrecruit.mapper.EnterpriseMapper;
import com.springboot_gradrecruit.mapper.JobMapper;
import com.springboot_gradrecruit.mapper.SendMapper;
import com.springboot_gradrecruit.model.dto.enterprise.AddInformationRequest;
import com.springboot_gradrecruit.model.dto.enterprise.PageInformationRequest;
import com.springboot_gradrecruit.model.dto.enterprise.UpdateInformationRequest;
import com.springboot_gradrecruit.model.entity.Enterprise;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.entity.Send;
import com.springboot_gradrecruit.model.entity.User;
import com.springboot_gradrecruit.model.vo.enterprise.EnterpriseVO;
import com.springboot_gradrecruit.model.vo.job.JobVO;
import com.springboot_gradrecruit.service.EnterpriseService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
* @author zr
* @description 针对表【enterprise】的数据库操作Service实现
* @createDate 2024-11-01 11:29:10
*/
@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise>
    implements EnterpriseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;


    @Resource
    private JobMapper jobMapper;

    @Resource
    private SendMapper sendMapper;


    @Override
    public Long addInformation(@RequestBody AddInformationRequest addInformationRequest, HttpServletRequest request) {
        // 1.获取当前登录用户
        User user = (User) request.getSession().getAttribute(addInformationRequest.getUserRole()+ addInformationRequest.getAccount());
        //2.检测当前用户是否有企业信息
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_user_id",user.getUserId());
        if(this.count(queryWrapper)>0){
            throw new CustomException("当前用户已有企业信息");
        }
        //3.添加企业信息
        Enterprise enterprise = new Enterprise();
        enterprise.setEnterpriseUserId(user.getUserId());
        enterprise.setEnterpriseCertificate(addInformationRequest.getEnterpriseCertificate());
        enterprise.setEnterpriseCertificateFile(String.join(",",addInformationRequest.getEnterpriseCertificateFile()));
        enterprise.setEnterpriseName(addInformationRequest.getEnterpriseName());
        enterprise.setEnterpriseLogoUrl(addInformationRequest.getEnterpriseLogoUrl());
        enterprise.setEnterpriseProfile(addInformationRequest.getEnterpriseProfile());
        enterprise.setEnterpriseIndustry(addInformationRequest.getEnterpriseIndustry());
        enterprise.setEnterpriseSize(addInformationRequest.getEnterpriseSize());
        enterprise.setEnterpriseFoundationDate(addInformationRequest.getEnterpriseFoundationDate());
        enterprise.setEnterpriseType(addInformationRequest.getEnterpriseType());
        enterprise.setEnterpriseAddress(addInformationRequest.getEnterpriseAddress());
        enterprise.setEnterpriseEmail(addInformationRequest.getEnterpriseEmail());
        enterprise.setEnterpriseWebsite(addInformationRequest.getEnterpriseWebsite());
        enterprise.setEnterpriseCertificationStatus(addInformationRequest.getEnterpriseCertificationStatus());
        enterprise.setEnterpriseSocialMediaLinks(addInformationRequest.getEnterpriseSocialMediaLinks());
        return (long) enterpriseMapper.insert(enterprise);
    }

    @Override
    public EnterpriseVO queryInformationByUserId(Long enterpriseUserId) {

        // 1.构建查询条件
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_user_id",enterpriseUserId);
        //2.执行查询
        Enterprise enterprise =enterpriseMapper.selectOne(queryWrapper);
        //3.返回
        return BeanUtil.copyProperties(enterprise,EnterpriseVO.class);

    }

    @Override
    public boolean updateInformationById(UpdateInformationRequest updateInformationRequest) {


        //1.获取参数
        Long enterpriseId = updateInformationRequest.getEnterpriseId();
        String enterpriseCertificate = updateInformationRequest.getEnterpriseCertificate();
        String enterpriseCertificateFile = updateInformationRequest.getEnterpriseCertificateFile();
        String enterpriseName = updateInformationRequest.getEnterpriseName();
        String enterpriseLogoUrl = updateInformationRequest.getEnterpriseLogoUrl();
        String enterpriseProfile = updateInformationRequest.getEnterpriseProfile();
        String enterpriseIndustry = updateInformationRequest.getEnterpriseIndustry();
        String enterpriseSize = updateInformationRequest.getEnterpriseSize();
        Date enterpriseFoundationDate = updateInformationRequest.getEnterpriseFoundationDate();
        String enterpriseType = updateInformationRequest.getEnterpriseType();
        String enterpriseAddress = updateInformationRequest.getEnterpriseAddress();
        String enterpriseEmail = updateInformationRequest.getEnterpriseEmail();
        String enterpriseWebsite = updateInformationRequest.getEnterpriseWebsite();
        String enterpriseSocialMediaLinks = updateInformationRequest.getEnterpriseSocialMediaLinks();
        Long enterpriseUserId = updateInformationRequest.getEnterpriseUserId();
        String enterpriseCertificationStatus= updateInformationRequest.getEnterpriseCertificationStatus();
        String enterpriseCertificationRefuse = updateInformationRequest.getEnterpriseCertificationRefuse();

        //todo 更新企业相关的岗位信息
        //1.获取岗位id
        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.eq("job_company_id",enterpriseUserId);
        List<Job> jobs = jobMapper.selectList(jobQueryWrapper);
        List<Long> jobIds = jobs.stream().map(Job::getJobId).toList();
        //2。更新岗位信息
        jobIds.forEach(jobId -> {
            Job job = new Job();
            job.setJobCompanyName(enterpriseName);
            job.setJobCompanySize(enterpriseSize);
            job.setJobCompanyIndustry(enterpriseIndustry);
            job.setJobCompanyLogo(enterpriseLogoUrl);
            jobMapper.update(job,new LambdaUpdateWrapper<Job>().eq(Job::getJobId,jobId));
        });

        //2.构建查询条件
        LambdaUpdateWrapper<Enterprise> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Enterprise::getEnterpriseId,enterpriseId)
                .set(enterpriseCertificateFile!=null,Enterprise::getEnterpriseCertificateFile,enterpriseCertificateFile)
                .set(enterpriseCertificate!=null,Enterprise::getEnterpriseCertificate,enterpriseCertificate)
                .set(StrUtil.isNotBlank(enterpriseName),Enterprise::getEnterpriseName,enterpriseName)
                .set(StrUtil.isNotBlank(enterpriseLogoUrl),Enterprise::getEnterpriseLogoUrl,enterpriseLogoUrl)
                .set(StrUtil.isNotBlank(enterpriseProfile),Enterprise::getEnterpriseProfile,enterpriseProfile)
                .set(StrUtil.isNotBlank(enterpriseIndustry),Enterprise::getEnterpriseIndustry,enterpriseIndustry)
                .set(StrUtil.isNotBlank(enterpriseSize),Enterprise::getEnterpriseSize,enterpriseSize)
                .set(enterpriseFoundationDate!=null,Enterprise::getEnterpriseFoundationDate,enterpriseFoundationDate)
                .set(StrUtil.isNotBlank(enterpriseType),Enterprise::getEnterpriseType,enterpriseType)
                .set(StrUtil.isNotBlank(enterpriseAddress),Enterprise::getEnterpriseAddress,enterpriseAddress)
                .set(StrUtil.isNotBlank(enterpriseEmail),Enterprise::getEnterpriseEmail,enterpriseEmail)
                .set(StrUtil.isNotBlank(enterpriseWebsite),Enterprise::getEnterpriseWebsite,enterpriseWebsite)
                .set(StrUtil.isNotBlank(enterpriseSocialMediaLinks),Enterprise::getEnterpriseSocialMediaLinks,enterpriseSocialMediaLinks)
                .set(StrUtil.isNotBlank(enterpriseCertificationStatus),Enterprise::getEnterpriseCertificationStatus,enterpriseCertificationStatus)
                .set(StrUtil.isNotBlank(enterpriseCertificationRefuse), Enterprise::getEnterpriseCertificationRefuse, enterpriseCertificationRefuse);
        return enterpriseMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public PageResponse<EnterpriseVO> pageInformation(PageInformationRequest pageInformationRequest) {

        //1.获取参数
        String enterpriseName = pageInformationRequest.getEnterpriseName();
        String enterpriseProfile = pageInformationRequest.getEnterpriseProfile();
        String enterpriseIndustry = pageInformationRequest.getEnterpriseIndustry();
        String enterpriseSize = pageInformationRequest.getEnterpriseSize();
        Date enterpriseFoundationDate = pageInformationRequest.getEnterpriseFoundationDate();
        String enterpriseType = pageInformationRequest.getEnterpriseType();
        String enterpriseAddress = pageInformationRequest.getEnterpriseAddress();
        String enterpriseEmail = pageInformationRequest.getEnterpriseEmail();
        String enterpriseWebsite = pageInformationRequest.getEnterpriseWebsite();
        String enterpriseSocialMediaLinks = pageInformationRequest.getEnterpriseSocialMediaLinks();
        Long enterpriseUserId = pageInformationRequest.getEnterpriseUserId();
        String enterpriseCertificationStatus = pageInformationRequest.getEnterpriseCertificationStatus();

        //2.构建查询条件
        Page<Enterprise> page = pageInformationRequest.toPageDefaultSortByUpdateTime();

        //3.执行查询
        Page<Enterprise> enterprisePage = lambdaQuery()
                .like(StrUtil.isNotBlank(enterpriseName), Enterprise::getEnterpriseName, enterpriseName)
                .like(StrUtil.isNotBlank(enterpriseProfile), Enterprise::getEnterpriseProfile, enterpriseProfile)
                .like(StrUtil.isNotBlank(enterpriseIndustry), Enterprise::getEnterpriseIndustry, enterpriseIndustry)
                .like(StrUtil.isNotBlank(enterpriseSize), Enterprise::getEnterpriseSize, enterpriseSize)
                .like(enterpriseFoundationDate!=null, Enterprise::getEnterpriseFoundationDate, enterpriseFoundationDate)
                .like(StrUtil.isNotBlank(enterpriseType), Enterprise::getEnterpriseType, enterpriseType)
                .like(StrUtil.isNotBlank(enterpriseAddress), Enterprise::getEnterpriseAddress, enterpriseAddress)
                .like(StrUtil.isNotBlank(enterpriseEmail), Enterprise::getEnterpriseEmail, enterpriseEmail)
                .like(StrUtil.isNotBlank(enterpriseWebsite), Enterprise::getEnterpriseWebsite, enterpriseWebsite)
                .like(StrUtil.isNotBlank(enterpriseSocialMediaLinks), Enterprise::getEnterpriseSocialMediaLinks, enterpriseSocialMediaLinks)
                .eq(enterpriseUserId!=null,Enterprise::getEnterpriseUserId,enterpriseUserId)
                .eq(StrUtil.isNotBlank(enterpriseCertificationStatus),Enterprise::getEnterpriseCertificationStatus,enterpriseCertificationStatus)
                .page(page);

        return PageResponse.of(enterprisePage, EnterpriseVO.class);
    }

    @Override
    public List<EnterpriseVO> queryHotEnterprise() {
        //1.查询出投递最多的五个岗位
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("enterprise_id,count(enterprise_id) as count")
                .groupBy("enterprise_id")
                .orderByDesc("count")
                .last("limit 5");
        List<Send> sends = sendMapper.selectList(queryWrapper);

        if(sends.size()==0){
            return null;

        }
        //2.根据岗位id查询企业信息
        QueryWrapper<Enterprise> enterpriseQueryWrapper = new QueryWrapper<>();
        enterpriseQueryWrapper.in("enterprise_id",sends.stream().map(Send::getEnterpriseId).toList());
        List<Enterprise> enterprises = enterpriseMapper.selectList(enterpriseQueryWrapper);
        List<EnterpriseVO> enterpriseVOS = enterprises.stream().map(enterprise -> {
            EnterpriseVO enterpriseVO = BeanUtil.copyProperties(enterprise, EnterpriseVO.class);
            return enterpriseVO;

        }).toList();
        return enterpriseVOS;
    }
}




